package com.hhsj.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.hhsj.ui.R;

import java.util.ArrayList;
import java.util.List;

//均分布局，设置布局的占比
public class EquallyLayout extends ViewGroup {

    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;
    private int splitNum = 2;
    private int orientation = HORIZONTAL;//水平
    private int spacing = 0;//间隙


    public EquallyLayout(Context context) {
        super(context);
    }

    public EquallyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public EquallyLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.EquallyLayout);
        if (array.hasValue(R.styleable.EquallyLayout_splitNum)) {
            splitNum = array.getInteger(R.styleable.EquallyLayout_splitNum, 1);
        }
        if (array.hasValue(R.styleable.EquallyLayout_android_orientation)) {
            orientation = array.getInt(R.styleable.EquallyLayout_android_orientation, HORIZONTAL);
        }

        if (array.hasValue(R.styleable.EquallyLayout_android_spacing)) {
            spacing = array.getDimensionPixelSize(R.styleable.EquallyLayout_android_spacing, 0);
        }
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //更新子视图的layoutParams，
        int childCount = getChildCount();
        List<View> notGoneViews = new ArrayList<>();
        int usedSize = 0;//使用的size
        for (int i = 0; i < childCount; i++) {
            if (getChildAt(i).getVisibility() != GONE) {
                notGoneViews.add(getChildAt(i));
                MarginLayoutParams params = (MarginLayoutParams) notGoneViews.get(i).getLayoutParams();
                if (orientation == VERTICAL) {
                    usedSize += params.topMargin;
                    usedSize += params.bottomMargin;
                } else {
                    usedSize += params.leftMargin;
                    usedSize += params.rightMargin;
                }
            }
        }

        if (notGoneViews.size() > 0) {
            usedSize = usedSize + spacing * (notGoneViews.size() - 1);
        }

        if (notGoneViews.size() >= splitNum) {
            splitNum = notGoneViews.size();
        }

        //剩余用来平分的高度
        int leftSize = Math.max(0, (orientation == VERTICAL ? getMeasuredHeight() - getPaddingTop() - getPaddingBottom() :
                getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) - usedSize);


        for (int i = 0; i < notGoneViews.size(); i++) {
            View child = notGoneViews.get(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();

            int widthSpec;
            int heightSpec;
            int childWidth;
            int childHeight;

            if (orientation == VERTICAL) {
                params.height = (int) (leftSize / (float) splitNum + 0.5f);
                childWidth = getMeasuredWidth() - (getPaddingLeft() + getPaddingRight()
                        + params.leftMargin + params.rightMargin);
                childHeight = params.height;

                if (params.width == LayoutParams.MATCH_PARENT) {
                    widthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
                } else {
                    widthSpec = getChildMeasureSpec(widthMeasureSpec,
                            getPaddingLeft() + getPaddingRight() +
                                    params.leftMargin + params.rightMargin,
                            params.width);
                }
                heightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);

            } else {
                params.width = (int) (leftSize / (float) splitNum + 0.5f);
                childWidth = params.width;
                childHeight = getMeasuredHeight() - (getPaddingTop() + getPaddingBottom()
                        + params.topMargin + params.bottomMargin);

                widthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);

                if (params.height == LayoutParams.MATCH_PARENT) {
                    heightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
                } else {
                    heightSpec = getChildMeasureSpec(heightMeasureSpec,
                            getPaddingTop() + getPaddingBottom() +
                                    params.topMargin + params.bottomMargin,
                            params.height);
                }
            }
            child.measure(widthSpec, heightSpec);
        }

    }

    @Override
    protected void onLayout(boolean force, int l, int t, int r, int b) {
        //更新子视图的layoutParams，

        int childCount = getChildCount();
        List<View> notGoneViews = new ArrayList<>();
        int usedSize = 0;
        for (int i = 0; i < childCount; i++) {
            if (getChildAt(i).getVisibility() != GONE) {
                notGoneViews.add(getChildAt(i));
            }
        }

        for (int i = 0; i < notGoneViews.size(); i++) {
            View child = notGoneViews.get(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            if (orientation == VERTICAL) {
                usedSize += params.topMargin;

                child.layout(getPaddingLeft() + params.leftMargin,
                        getPaddingTop() + usedSize,
                        getPaddingLeft() + params.leftMargin + child.getMeasuredWidth(),
                        getPaddingTop() + usedSize + params.height
                );

                usedSize += params.height;
                usedSize += params.bottomMargin;
            } else {
                usedSize += params.leftMargin;

                child.layout(getPaddingLeft() + usedSize,
                        getPaddingTop() + params.topMargin,
                        getPaddingLeft() + usedSize + params.width,
                        getPaddingTop() + params.topMargin + child.getMeasuredHeight()
                );

                usedSize += params.width;
                usedSize += params.rightMargin;
            }
            usedSize += spacing;
        }

    }

    @Override
    protected boolean checkLayoutParams(LayoutParams p) {
        return p instanceof MarginLayoutParams;
    }

    @Override
    public MarginLayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected MarginLayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    protected MarginLayoutParams generateDefaultLayoutParams() {
        if (orientation == VERTICAL) {
            return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    }

    public void setSplitNum(int splitNum) {
        if (splitNum <= 1) {
            splitNum = 1;
        }
        this.splitNum = splitNum;
    }

    //方向
    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }

    //间隙
    public void setSpacing(int space) {
        this.spacing = space;
    }
}
